package net.borderwars.io;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.logging.Logger;

/**
 * @author Eric
 *         Date: Jul 26, 2005
 *         Time: 4:40:12 PM
 */
public class MeasuredOutputStream extends FilterOutputStream {
    static private Logger log = Logger.getLogger ("MeasuredOutputStream");
    long createdAt;
    int totalBytes = 0;

    public MeasuredOutputStream (OutputStream out) {
        super (out);
        createdAt = System.currentTimeMillis ();
    }

    public long getBytesPerSecond () {
        long time = System.currentTimeMillis () - createdAt;
        if (time == 0)
            time = 1;
        return (totalBytes / time);
    }

    public void write (int b) throws IOException {
        super.write (b);
        totalBytes++;
    }

    public void write (byte b[]) throws IOException {
        super.write (b);
        totalBytes += b.length;
    }

    public void write (byte b[], int off, int len) throws IOException {
        super.write (b, off, len);    //To change body of overridden methods use File | Settings | File Templates.
        totalBytes += len;
    }
}
